﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class ChucdanhDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" SELECT *,b.TENQUYEN FROM chucdanh a join quyen b on a.maquyen=b.maquyen";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM chucdanh WHERE macd = '{0}'";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (chucdanh)obj;
            string sql = @"INSERT INTO chucdanh(macd,tencd,maquyen) VALUES('{0}',N'{1}','{2}')";
            sql = string.Format(sql,o.macd,o.tencd,o.maquyen);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (chucdanh)obj;
            string sql = @"UPDATE chucdanh SET tencd=N'{0}', maquyen='{1}' where macd='{2}'";
            sql = string.Format(sql,o.tencd,o.maquyen,o.macd);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_quyen()
        {
            string sql = @" SELECT maquyen as id, tenquyen as ten FROM quyen";
            return ExecuteQuery(sql);
        }
        public string Select_max()
        {
            string sql = @"SELECT max(substring(macd,3,LEN(macd)-2)) as id FROM chucdanh";
            DataTable dt= ExecuteQuery(sql);
            return dt.Rows.Count > 0 ? dt.Rows[0]["id"] + "" : "0";
        }



        public bool deleteobj(object obj)
        {
            throw new NotImplementedException();
        }
    }
}
